package pl.pr3sto.security

import grails.test.*

class ProfileTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

	void testToString() {
		mockDomain(Person)
		mockDomain(Profile)
		
		// given
		Person person = new Person(email: 'email@afas.pl', firstName: 'Zuzanna', lastName: 'Kuna', city: 'aaa', address: 'aaa', phone: '123', postCode: '123')
		Profile profile = new Profile(login: 'login1', password: 'password', person: person)
		
		// when/then
		assert profile.toString() == 'Zuzanna Kuna'
	}
	
    void testConstraintLoginUnique() {
		mockForConstraintsTests(Profile)
		mockDomain(Profile)
		
		// given
		new Profile(login: 'login1', password: 'password', person: new Person()).save()
		Profile newProfile = new Profile(login: 'login1', password: 'password', person: new Person())
		
		// when
		newProfile.validate()
		
		// then
		assert newProfile.errors.getFieldError('login').code == 'unique'
    }

    void testConstraintLoginBlank() {
		mockForConstraintsTests(Profile)
		mockDomain(Profile)
		
		// given
		Profile newProfile = new Profile(login: '', password: 'password', person: new Person())
		
		// when
		newProfile.validate()
		
		// then
		assert newProfile.errors.getFieldError('login').code == 'blank'
    }
}
